package com.qk.controller;

import com.qk.anno.Log;
import com.qk.dto.UserDto;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @GetMapping
    public Result list(UserDto userDto) {
        log.info("查询对象: {}" , userDto );
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
    @Log
    @PostMapping
    public Result add(@RequestBody User user) {
        log.info("添加用户: {}" , user );
        userService.add(user);
        return Result.success();
    }
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids) {
        log.info("删除用户: {}" , ids );

        userService.deleteByIds(ids);

        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询用户 {}" ,  id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }
    @Log
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户: {}" , user );
        userService.updateUser(user);
        return Result.success();
    }

    //根据角色标识查询
    @GetMapping("/role/{roleLabel}")
    public Result listByRoleLabel(@PathVariable String roleLabel) {
        log.info("根据角色标识查询: {}" , roleLabel);
        List<User> users = userService.listByRoleLabel(roleLabel);
        return Result.success(users);
    }
    @GetMapping("/dept/{deptId}")
    public Result listByDeptId(@PathVariable Integer deptId) {
        log.info("根据部门id查询: {}" , deptId);
        List<User> users = userService.listByDeptId(deptId);
        return Result.success(users);
    }
}
